package com.third.clock.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.third.clock.R;
import com.third.clock.bean.NoticeEntity;
import com.third.clock.ui.model.INoticeListView;
import com.third.clock.ui.model.adapter.NoticeListAdapter;
import com.third.clock.ui.model.presenter.MindsFragmentPresenter;
import com.third.clock.ui.model.presenter.NoticeListPresenter;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

/**
 * Created by Administrator on 2018/8/9 0009.
 */

public class NoticeListActivity extends Activity implements INoticeListView,SwipeRefreshLayout.OnRefreshListener,SwipeMenuRecyclerView.LoadMoreListener,SwipeItemClickListener {

    private SwipeMenuRecyclerView mRecyclerView;

    private SwipeRefreshLayout mSwiperefreshlayout;

    private View headView;

    private TextView mTextViewTitle;

    private NoticeListPresenter presenter;

    private NoticeListAdapter adapter;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticelist);
        initView();
        initSwipeRefreshLayout();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(presenter!=null) presenter.onRefresh(NoticeListPresenter.LoadType.REFRESH);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void startRefreshing() {
        mSwiperefreshlayout.setRefreshing(true);
    }

    @Override
    public void stopRefreshing() {
        mSwiperefreshlayout.setRefreshing(false);
    }

    @Override
    public void onLoadmorestop() {
        // 数据完更多数据，一定要掉用这个方法。
        // 第一个参数：表示此次数据是否为空。
        // 第二个参数：表示是否还有更多数据。
        mRecyclerView.loadMoreFinish(false, true);
    }

    @Override
    public void onRefreshData(NoticeListPresenter.LoadType type, List<NoticeEntity> list) {
        if(adapter!=null) {
            if(type== NoticeListPresenter.LoadType.REFRESH) {
                adapter.setList(list);
                mRecyclerView.loadMoreFinish(false, true);
            }else{
                adapter.addList(list);
            }
        }
    }

    @Override
    public void onRefresh() {
        if(presenter!=null) presenter.onRefresh(NoticeListPresenter.LoadType.REFRESH);
    }

    @Override
    public void onLoadMore() {
        if(presenter!=null) presenter.onRefresh(NoticeListPresenter.LoadType.LOADMORE);
    }

    @Override
    public void onItemClick(View itemView, int position) {
       if(adapter!=null) {
           NoticeDetailsActivity.intentData(NoticeListActivity.this,adapter.getItem(position));
       }
    }

    private void initView(){
        headView=findViewById(R.id.noticelist_headview);
        mTextViewTitle= (TextView) headView.findViewById(R.id.textview_head_title);
        mTextViewTitle.setText("公告");

        mRecyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerview);
        mSwiperefreshlayout= (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout);
        mSwiperefreshlayout.setOnRefreshListener(this);
        LinearLayoutManager manager=new LinearLayoutManager(NoticeListActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setSwipeItemClickListener(this); // RecyclerView Item点击监听。
        mRecyclerView.useDefaultLoadMore(); // 使用默认的加载更多的View。
        mRecyclerView.setLoadMoreListener(this); // 加载更多的监听。


        presenter=new NoticeListPresenter(NoticeListActivity.this);
        presenter.setINoticeListView(this);

        adapter=new NoticeListAdapter(NoticeListActivity.this);
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 初始化下拉刷新控件，SwipeRefreshLayout
     */
    private void initSwipeRefreshLayout() {
        //设置刷新进度条的颜色变化，最多可以设置 4 种，加载的颜色是循环播放的。
        mSwiperefreshlayout.setColorSchemeResources(R.color.colorAccent);
        //设置手指在屏幕上下拉多少会触发下拉刷新
        mSwiperefreshlayout.setDistanceToTriggerSync(300);
        //设置下拉刷新的圆圈背景颜色
        mSwiperefreshlayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        //设置下拉刷新的圆圈大小
        mSwiperefreshlayout.setSize(SwipeRefreshLayout.DEFAULT);
        mSwiperefreshlayout.setOnRefreshListener(this);

    }

}
